/*
 * This file is a part of ShtutXML
 * © Barak Itkin <lightningismyname at gmail dot com>, 2011
 *
 * ShtutXML is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.shtutxml;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * An example class that shows how to use this package
 */
public class Example {

    public static void PrintDoc (Node e)
    {
        new DocumentPrinter().visit(e, DepthDocumentVisitor.VisitOrder.DEPTH_FIRST);
    }

    public static StrXML Parse (File xmlFile) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(xmlFile);

        return StrXML.parseDocument (doc);
    }

    public static void main(String[] args) {
        try {
            URL url = ClassLoader.getSystemClassLoader().getResource("test.xml");
            StrXML demo = Example.Parse(new File(url.toURI()));


            System.out.println("************* BASE DOCUMENT *****************");
            PrintDoc (demo.XMLDocument);
            
            System.out.println("\n*** Text ***");
            System.out.println("\"" + demo.GetText() + "\"");

            System.out.println("\n*** Node of each text segment ***");
            System.out.println(demo.debug());

            System.out.println("\n*** Offset testing ***");
            System.out.println("offset " + 0 + " is at " + demo.GetOffset(0));
            System.out.println("offset " + 40 + " is at " + demo.GetOffset(40));
            System.out.println("offset " + 80 + " is at " + demo.GetOffset(80));

            System.out.println();


            System.out.println("*********** Manipulation *****************");
//            demo.GetOffset(3).textSection.wrapWith(demo.XMLDocument.createElement("u"));
//            demo.splitSectionUpTo(demo.GetOffset(2), demo.GetOffset(2).getParentNode().getParentNode());
            demo.insertAndSplitInserted(demo.XMLDocument.createElement("u"), demo.GetOffset(2), demo.GetOffset(9));
            demo.insertAndSplitInserted(demo.XMLDocument.createElement("k"), demo.GetOffset(1), demo.GetOffset(4));

            PrintDoc  (demo.XMLDocument);
            
            demo.insertAndSplitParent(demo.XMLDocument.createElement("span"), demo.GetOffset(0), demo.GetOffset(6));

            PrintDoc  (demo.XMLDocument);
            System.out.println("\n*** Text ***");
            System.out.println("\"" + demo.GetText() + "\"");

            System.out.println("\n*** Node of each text segment ***");
            System.out.println(demo.debug());

            System.out.println("\n*** Offset testing ***");
            System.out.println("offset " + 0 + " is at " + demo.GetOffset(0));
            System.out.println("offset " + 40 + " is at " + demo.GetOffset(40));
            System.out.println("offset " + 80 + " is at " + demo.GetOffset(80));
        } catch (URISyntaxException ex) {
            Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Example.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
